﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebGui {

    /// <summary>
    /// The class for the Default.aspx page
    /// </summary>
    public partial class Default : Page {

        /// <summary>
        /// The event then the page is loading
        /// </summary>
        /// <param name="sender">The sender of the event</param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e) {
            if(Request.IsAuthenticated)
                Response.Redirect("~/User/Start.aspx");
            HttpCookie cookie = Request.Cookies["loginInfo"];
            if(cookie != null) {
                Login login = GetLogin();
                login.RememberMeSet = true;
                GetTextBox("UserName").Attributes.Add("value", cookie["username"]);
                GetTextBox("Password").Attributes.Add("value", cookie["password"]);
            }
        }

        /// <summary>
        /// The click event from the login button
        /// </summary>
        /// <param name="sender">The login button</param>
        /// <param name="e"></param>
        protected void BtnLoginClick(object sender, EventArgs e) {
            if(GetCheckBox("RememberMe").Checked)
                SetCookie();
            else
                DeleteCookie();
        }

        /// <summary>
        /// Get the login control
        /// </summary>
        /// <returns>The login control</returns>
        private Login GetLogin() {
            Login login = (Login) LoginViewDefault.FindControl("LoginBox");
            return login;
        }

        /// <summary>
        /// Get the checkbox from an id
        /// </summary>
        /// <param name="id">The id of the checkbox</param>
        /// <returns>The checkbox</returns>
        private CheckBox GetCheckBox(string id) {
            Login login = GetLogin();
            CheckBox checkbox = (CheckBox) login.FindControl(id);
            return checkbox;
        }

        /// <summary>
        /// Get the textbox from an id
        /// </summary>
        /// <param name="id">The id of the textbox</param>
        /// <returns>The textbox</returns>
        private TextBox GetTextBox(string id) {
            Login login = GetLogin(); // (Login) LoginViewDefault.FindControl("LoginBox");
            TextBox textBox = (TextBox) login.FindControl(id);
            return textBox;
        }

        /// <summary>
        /// Add the username and password to a cookie
        /// </summary>
        private void SetCookie() {
            HttpCookie cookie = new HttpCookie("loginInfo");
            cookie["username"] = GetTextBox("UserName").Text;
            cookie["password"] = GetTextBox("Password").Text;
            cookie.Expires = DateTime.Now.AddYears(1);
            Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// Delete a cookie
        /// </summary>
        private void DeleteCookie() {
            HttpCookie cookie = new HttpCookie("loginInfo");
            cookie.Expires = DateTime.Now.AddYears(-1);
            Response.Cookies.Add(cookie);
        }
    }
}